package com.hns.can.energy.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.SpeedAnalysisService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.can.energy.vo.SpeedAnalysisVo;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 转速分析controller
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/speedAnalysis")
public class SpeedAnalysisController extends BaseController {
	
	@Autowired
	private SpeedAnalysisService speedAnalysisService;
	
	private String[] tableHeaderName={"驾驶员","车牌号","车辆编号","营运线路","所属机构","运营日期","运行时长","运行里程","燃料类型","运行能耗",
			"百公里能耗(L|m³|KWh/100KM)","能耗排名","怠速时长",
			"怠速时长百分比","空调怠速时长","空调怠速时长百分比","低转时长","低转时长百分比","绿区时长","绿区时长百分比","高转时长",
			"高转时长百分比","超转时长","超转时长百分百","最经济转速时长","最经济转速时长百分比","反转时长","反转时长百分比"};
	
	private String[] tablePropertyName = {"drvName","licPltNo","carInCd","lineName","organName","rcrdTime","totRngOprtDura","totRngMile","fuelType",
			"totRngEnerCons","enerConsPtg","enerRank",
			"idliDuraStr","idliDuraRate","airCondiIdliDuraStr","airCondiIdliDuraRate","slowSpdRevoDuraStr",
			"slowSpdRevoDuraRate","lowEnerAreaDriveDuraStr","lowEnerAreaDriveDuraRate","highSpdRevoDuraStr","highSpdRevoDuraRate",
			"suspeedDuraStr","suspeedDuraRate","mostEcnRtteSpdDuraStr","mostEcnRtteSpdDuraRate","revDuraStr","revDuraRate"};
	
	/**
	 * 查询转速分析
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("/searchSpeedAnalysis")
	@ResponseBody
    public Object searchSpeedAnalysis(NewEnergy vo) throws Exception {
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
        	throw new BusinessException(MsgType.ERROR.getFlag(), "参数异常");
        }
        vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        Pagination page = speedAnalysisService.searchSpeedAnalysis(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
	
	/**
	 * 导出转速excel
	 * @param vo
	 * @param response
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("/exportSpeedAnalysisExcel")
    public void exportSpeedAnalysisExcel(NewEnergy vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List<SpeedAnalysisVo> list = speedAnalysisService.exportSpeedAnalysis(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportUtil.exportFiles(Arrays.asList(tableHeaderName), Arrays.asList(tablePropertyName), list, response, "excel", 
        		URLEncoder.encode("转速分析", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "转速分析");
    }
	
	/**
	 * 导出转速pdf
	 * @param vo
	 * @param response
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("/exportSpeedAnalysisPdf")
    public void exportSpeedAnalysisPdf(NewEnergy vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List<SpeedAnalysisVo> list = speedAnalysisService.exportSpeedAnalysis(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportUtil.exportFiles(Arrays.asList(tableHeaderName), Arrays.asList(tablePropertyName), list, response, "pdf", 
        		URLEncoder.encode("转速分析", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "转速分析");
    }
	
	/**
	 * 查询线路图表
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("/searchLineEnergyChart")
	@ResponseBody
    public Object searchLineEnergyChart(NewEnergy vo) throws Exception {
        SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = speedAnalysisService.searchLineEnergyChart(vo);
        return this.getReturnResult(list);
    }
	
	
	
}
